package com.sq.partner.manager.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.RuleInfoDao;
import com.sq.partner.manager.mybatis.model.TGatewayInvoice;
import com.sq.partner.manager.util.Page;

@Repository
public class RuleInfoDaoImpl extends BaseDaoImpl implements RuleInfoDao{
	
	@Override
	public List<Map<String , String >> queryNlifePlans(String deptCode) throws SQLException {
		Map<String,String> map = new HashMap<String, String>();
		map.put("value", deptCode==null?"''":deptCode.substring(1, 3) );
		return this.queryList("queryPlansOptions", map);
	}

	@Override
	public boolean deletOldInfoByPlanNo(String planNo) throws Exception {
		return this.delete("deletOldInfoByPlanNo", planNo);
	}

	@Override
	public void insertNewPlanInfo(Map<String, String> map) throws Exception {
		String planNo = map.get("planNo");
		map.remove("planNo");
		Set<String> keys = map.keySet();
		
		for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			Map<String,String> nmap =new HashMap<String, String>();
			nmap.put("type", key);
			nmap.put("text", map.get(key));
			nmap.put("planNo", planNo);
			this.add("insertNewPlanInfo", nmap);
		}
		
	}

	@Override
	public Map<String, String> queryPlanInfosByPlanNo(String planNo)
			throws Exception {
		List<Map<String,String>> rsList = this.queryList("queryPlanInfosByPlanNo", planNo);
		Map<String,String> rsMap = new HashMap<String, String>();
		for (Iterator iterator = rsList.iterator(); iterator.hasNext();) {
			Map<String, String> map = (Map<String, String>) iterator.next();
			rsMap.put(map.get("INFO_TYPE"), map.get("INFO_DETAIL"));
		}
		return rsMap;
	}

	@Override
	public boolean insertNewRiskContent(Map<String, String> map) throws Exception {
		
		this.delete("deleteExistRiskFile", map.get("riskCode"));
		
		return this.add("insertNewRiskContent", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, String>> loadRiskContentList(Map<String, Object> map)
			throws Exception {
		List<Map<String, String>> rsList = new ArrayList<Map<String,String>>();
		rsList = this.queryList("loadRiskContentList", map);
		return rsList;
	}

	@Override
	public int loadRiskContentListCount(Map<String, Object> map) throws Exception{
		return (Integer) this.query("loadRiskContentListCount", map);
	}

	@Override
	public boolean isRiskFileExist(String riskCode) throws SQLException {

		//判断条款文件是否已经存在
		int count = (Integer) this.query("isRiskFileExist", riskCode);
		if(count > 0){
			return true;
		}
		else{
			return false;
		}
	}

	@Override
	public String previewRiskContent(String riskCode) throws Exception {
		return (String) this.query("previewRiskContent", riskCode);
	}
	
	
	@Override
    public Page queryPlanPageByCriteria(Page page) throws Exception {     
        Map<String,Object> map = (Map<String, Object>) page.getType();
        String value = map.get("deptCode")+"";
        page.setTotal((Integer)this.query("queryPlansCounts", map));
        List<TGatewayInvoice> list = queryList("queryPlansPageByMap", map);
        page.setData(list); 
        return page;
    }
	
}
